package com.ce.pms.house.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.house.dao.IChargePrecinctDao;
import com.ce.pms.house.model.BuildingDomain;
import com.ce.pms.house.model.ChargePrecinctDomain;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

public class ChargePrecinctDaoImpl extends PmsBaseDaoImpl<ChargePrecinctDomain> implements IChargePrecinctDao {

	/**
	 * 方法描述: 根据管理处ID分页查询其下所有管理区
	 * @author 胡煦
	 * Create on:2008-7-3
	 * @param qc
	 * @param pageNo
	 * @param pageSize
	 * @return page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	 public Page queryPagePrecinct(Long id, int pageNo, int pageSize) {
		 if(BlankUtil.isBlank(id)){
				return null;
			}
		 String queryStr =  "from ChargePrecinctDomain cp where cp.manageOrg.keyword=? order by cp.sysInfo.sysDate";

		 return this.pagedQueryByPageNo(queryStr, pageNo, pageSize,new Object[]{id});

	 }
	 /**
		 * 功能描述:根据管理区名字查询记录数<br>
		 * 注：新建判断name的唯一性时，oldName传为null；修改判断name的唯一性时，则需要传入修改前的名字<br>
		 * @param precinctName
		 * @param oldName
		 * @return
		 * Create author:胡煦
		 * Create on:2008-7-18
		 * Edit author:
		 * Edit on:
		 * Why:
		 */
		public Long checkPrecinctByName(String precinctName,String oldName){
			StringBuffer hql=new StringBuffer();
			List list = new ArrayList();
			list.add(precinctName);

			hql.append("select count(*) FROM  Precinct pc WHERE pc.name = ? ");
			if(oldName!=null){
				hql.append(" and pc.name <> ?");
				list.add(oldName);
			}
			Query query=this.createQuery(hql.toString(), list.toArray());
			Object result=query.uniqueResult();

			return (result == null ? null : (Long)result );
		}

		/**
		 * 功能描述:根据管理区编号查询记录数<br>
		 * 注：新建判断管理区编号的唯一性时，oldCode传为null；修改判断管理区编号的唯一性时，则需要传入修改前的code<br>
		 * @param precinctCode
		 * @param oldCode
		 * @return
		 * Create author:胡煦
		 * Create on:2008-9-17
		 * Edit author:
		 * Edit on:
		 * Why:
		 */
		public Long checkPrecinctByCode(String precinctCode,String oldCode){
			StringBuffer hql=new StringBuffer();
			List list = new ArrayList();
			list.add(precinctCode);

			hql.append("select count(*) FROM  Precinct pc WHERE pc.precinctCode = ? ");
			if(oldCode!=null){
				hql.append(" and pc.precinctCode <> ?");
				list.add(oldCode);
			}
			Query query=this.createQuery(hql.toString(), list.toArray());
			Object result=query.uniqueResult();

			return (result == null ? null : (Long)result );
		}

		/**
		 * 方法描述: 根据楼栋查询当前收费管理区
		 * @author 胡煦
		 * Create on:2008-8-18
		 * @param
		 * @return ChargePrecinctDomain
		 * Edit author:
		 * Edit on:
		 * 修改原因：
		 */
		public ChargePrecinctDomain findChargePrecinctBy(Long buildingId){
			 String queryStr =  "SELECT * FROM  HM_CHARGE_PRECINCT_R_BUILDING cb  WHERE cb.BUILDING_ID = ? ";
			 Object[] object =  (Object[]) this.createSQLQuery(queryStr,buildingId).uniqueResult();
			 if(BlankUtil.isBlank(object)){
				 return null;
			 }
			 BigInteger l = (BigInteger) object[0];
			 return this.findById(l.longValue());
		}


		/**
		 *
		 * 功能描述: 得到所有已被管理区引用的楼栋
		 *
		 * @author 胡煦
		 * Created on:2008-7-21
		 *
		 * 修改原因：
		 * @param
		 * @return List<BuildingDomain>
		 */
		public List<BuildingDomain> getBuildings() {
			List<BuildingDomain> resultBuildingList = new ArrayList<BuildingDomain>();
			List<ChargePrecinctDomain> resultList = new ArrayList<ChargePrecinctDomain>();
			resultList = this.findAll();
			for(ChargePrecinctDomain c: resultList) {
				Set<BuildingDomain> buildings = c.getBuildings();
				resultBuildingList.addAll(buildings);
			}
			return resultBuildingList;
		}
}
